package cc.iits.demo.crane.web.config;

import cc.iits.demo.crane.web.support.StringToDateConverter;
import cc.iits.demo.crane.web.support.interceptor.SessionHandlerInterceptor;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * spring mvc 配置
 * @author 易俊
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Bean
    public SessionHandlerInterceptor session(){
        return new SessionHandlerInterceptor();
    }

//    @Bean
//    public AuthorizationHandlerInterceptor authorization() {
//        return new AuthorizationHandlerInterceptor();
//    }
//
//    @Bean
//    public AuthenticationHandlerInterceptor authentication() {
//        return new AuthenticationHandlerInterceptor();
//    }

    /**
     * 添加拦截器
     *
     * @param registry
     */
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        //会话拦截
        registry.addInterceptor(session())
                .addPathPatterns("/v1/**");

        //登录授权
//        registry.addInterceptor(authorization())
//                .addPathPatterns(apiInterceptorUrl)
//                .excludePathPatterns(excludeWebPath);

        //权限认证
//        registry.addInterceptor(authentication())
//                .addPathPatterns(apiInterceptorUrl);
//        registry.addInterceptor(loggerHandlerInterceptor).addPathPatterns("/api/**");
        super.addInterceptors(registry);
    }

    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        /*修改默认页*/
        registry.addViewController("/").setViewName("forward:/index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //将所有/static/** 访问都映射到classpath:/static/ 目录下
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }

    @Override
    protected void addFormatters(FormatterRegistry registry) {
        //添加字符串转日期
        registry.addConverter(new StringToDateConverter());
    }

    /**
     * 数据转换工具
     *
     * @param converters
     */
    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
//        config.setDateFormat(Constants.DATETIME_PATTERN);
        config.setSerializerFeatures(
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.NotWriteRootClassName,
                SerializerFeature.BrowserCompatible);
        converter.setFastJsonConfig(config);

        List<MediaType> mediaTypes = new ArrayList();
        mediaTypes.add(MediaType.APPLICATION_JSON);
//        mediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        converter.setSupportedMediaTypes(mediaTypes);

        converters.add(converter);
        super.configureMessageConverters(converters);
    }

    @Override
    protected void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowedOrigins("*")
                .exposedHeaders("X-Auth-Token");
    }
}
